package network;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Iterator;

public class MyJson implements IJson {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    JSONObject root = null;

    public MyJson(String jsonString) {
        try {
            root = new JSONObject(jsonString);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public MyJson(JSONObject jsonObject) {
        root = jsonObject;
    }

    public MyJson() {
        root = new JSONObject();
    }

    public JSONObject getRoot() {
        return root;
    }

    public boolean put(String key, int value) {
        try {
            root.put(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean put(String key, Object value) {
        try {
            root.put(key, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public String toString() {
        if (root == null) {
            return null;
        }
        return root.toString();
    }

    @Override
    public boolean isNull(String name) {
        try {
            return root.isNull(name);
        } catch (Exception e) {
            return true;
        }
    }

    public String getString(String key) {
        try {
            return root.get(key).toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public int getInt(String key) {
        try {
            return root.getInt(key);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    public boolean has(String key) {
        try {
            return root.has(key);
        } catch (Exception e) {

            e.printStackTrace();
        }
        return false;
    }

    @SuppressWarnings("unchecked")
    public Iterator<String> keys() {
        return (Iterator<String>) root.keys();
    }

    public String firstKey() {
        return keys().next();
    }

    public boolean getBoolean(String key) {
        try {
            return root.getBoolean(key);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public MyJson getJson(String key) {
        try {
            return new MyJson(root.getJSONObject(key));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public MyJson[] getJsonArray(String key) {
        MyJson[] myJsons;
        try {
            JSONArray a = root.getJSONArray(key);
            myJsons = new MyJson[a.length()];
            for (int i = 0; i < myJsons.length; i++) {
                myJsons[i] = new MyJson(a.getJSONObject(i));
            }

            return myJsons;
        } catch (Exception e) {
//            e.printStackTrace();
            return null;
        }
    }

    public String[] getArray(String key) {
        String[] strs;
        try {
            JSONArray a = root.getJSONArray(key);
            strs = new String[a.length()];
            for (int i = 0; i < strs.length; i++) {
                strs[i] = a.getString(i);
            }
            return strs;
        } catch (Exception e) {
            return null;
        }
    }

}
